package com.ucomm.talent.activity;

import java.util.ArrayList;
import java.util.Date;

import me.maxwin.view.IXListViewLoadMore;
import me.maxwin.view.IXListViewRefreshListener;
import me.maxwin.view.XListView;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.ucomm.talent.AppConfig;
import com.ucomm.talent.AppConstants;
import com.ucomm.talent.AppException;
import com.ucomm.talent.R;
import com.ucomm.talent.activity.detail.HotComDetailActivity;
import com.ucomm.talent.adapter.HotCompanyListAdapter;
import com.ucomm.talent.common.DebugUtil;
import com.ucomm.talent.manager.SearchManager;
import com.ucomm.talent.model.HotCompanyInfo;
import com.ucomm.talent.widget.CustomProgressDialog;
import com.ucomm.talent.widget.MainHeaderBar;
/**
 * 热门企业
 * @author Administrator
 *
 */
public class HotCompanyActivity extends BaseActivity implements  IHeaderBarClickListener{
	
	private XListView listView;
	private MainHeaderBar mainHeaderBar;
	private SearchManager searchManager;
	private CustomProgressDialog customProgressDialog;
	private HotCompanyListAdapter adapter; 
	private ArrayList<HotCompanyInfo> data;
	private String url = null;
	
	Handler mHandler = new Handler(){
	@Override
    public void handleMessage(Message msg) {
        int what = msg.what;
        DebugUtil.Log(HotCompanyActivity.this,"---------- " + customProgressDialog.toString());
        switch (what){
            case AppConstants.MsgStatus.MSG_ERROR:
                if (customProgressDialog != null && customProgressDialog.isShowing())
                    customProgressDialog.dismiss();
                ((AppException)msg.obj).makeToast(HotCompanyActivity.this);
                break;
            case AppConstants.MsgStatus.MSG_SUCCUSS:
                if (customProgressDialog != null && customProgressDialog.isShowing())
                    customProgressDialog.dismiss();
	                //放置数据
	                data.addAll((ArrayList<HotCompanyInfo>) msg.obj);
	                if(adapter==null){
		                adapter = new HotCompanyListAdapter(HotCompanyActivity.this, data);
		                listView.setAdapter(adapter);
	                }else{
	                	adapter.notifyDataSetChanged();
	                }
                //设置监听
                listView.setOnItemClickListener(new ListItemListener());
                //上拉加载更多
                listView.setPullLoadEnable(new IXListViewLoadMore() {
                    @Override
                    public void onLoadMore() {
                    	if(AppConfig.CURR_PAGE !=0){
                    		int i = AppConfig.CURR_PAGE;
                    		if(i<AppConfig.PAGE_COUNT){
                    			AppConfig.CURR_PAGE += 1;
                    			String pageUrl = url+"&page="+AppConfig.CURR_PAGE;
                            	searchManager.searchDataFromNetwrok(pageUrl);
                    		}else{
                    			listView.disablePullLoad();
                    		}
                    	}
                    }
                });
                //下拉刷新
                listView.setPullRefreshEnable(new IXListViewRefreshListener() {
                    @Override
                    public void onRefresh() {
                    	searchManager.searchDataFromNetwrok(url);
                    	listView.stopRefresh(AppConfig.formatter.format(new Date(System.currentTimeMillis())));
                    }
                });
                break;
            case AppConstants.MsgStatus.MSG_LOADING:
                if (customProgressDialog != null && !customProgressDialog.isShowing())
                    customProgressDialog.show();
                break;
            default:
                break;
        		}
            super.handleMessage(msg);
	        }
	    };
	    public void onCreate(Bundle savedInstanceState) {
		    super.onCreate(savedInstanceState);
		    setContentView(R.layout.hotcompany_layout);
		    initView();
		    searchManager = new SearchManager(this, mHandler);
		    Intent i = getIntent();
		    String otherUrl = i.getStringExtra("url");
		    if (otherUrl!=null){
		    	url = otherUrl;
		    }else{
		    	url = AppConstants.HOT_COMPANY;
		    }
		    searchManager.searchDataFromNetwrok(url);
	    }
	    private void initView(){
	        mainHeaderBar = (MainHeaderBar) findViewById(R.id.main_header_bar);
	        mainHeaderBar.setBarClickListener(this);
	        customProgressDialog = new CustomProgressDialog(this);

	        listView = (XListView) findViewById(R.id.custom_list);
	        listView.setmHeaderViewBackground(R.color.custom_list_item_bg_color);
	        listView.setmFooterViewBackground(R.color.custom_list_item_bg_color);
	        data = new ArrayList<HotCompanyInfo>();

	    }
	class ListItemListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			ArrayList<HotCompanyInfo> mData = adapter.getmData();
			HotCompanyInfo info = mData.get(arg2-1);
			Intent i = new Intent();
			i.putExtra("info", info);
			i.setClass(HotCompanyActivity.this, HotComDetailActivity.class);
			startActivity(i);
		}
		
	}
	@Override
	public void onBackButtOnClick(View view) {
		// TODO Auto-generated method stub
		finish();
	}

	@Override
	public void onExitButtonOnClick(View view) {
		// TODO Auto-generated method stub
		
	}

}